package com.ocom.payhandle.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.PayHandleUrl;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payhandle.GetRecentOrderRecordsRequest;
import com.ocom.common.request.ugcapi.order.RecentAddressRequest;
import com.ocom.common.request.ugcapi.order.SelectByOrderNosRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payhandle.mq.business.saveorder.SaveOrderProductService;
import com.ocom.payhandle.service.XfRecordProcessService;
import com.ocom.payhandle.vo.MqSaveOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
public class RecordProcessController {

	@Autowired
	XfRecordProcessService xfRecordProcessService;

	@Autowired
	private SaveOrderProductService saveOrderProductService;

	@PostMapping(PayHandleUrl.URL_getRecentOrderRecords)
	public Result getRecentOrderRecords(@RequestBody GetRecentOrderRecordsRequest request){
		return ResultUtil.success(xfRecordProcessService.getRecentOrderRecords(request.getCompanyId(), request));
	}

	@PostMapping(PayHandleUrl.URL_getRecentAddress)
	public Result getRecentAddress(@RequestBody RecentAddressRequest request){
		return ResultUtil.success(xfRecordProcessService.getRecentAddress(request.getComId(), request));
	}

	@PostMapping(PayHandleUrl.URL_selectByOrderNos)
	public Result selectByOrderNos(@RequestBody SelectByOrderNosRequest request){
		return ResultUtil.success(xfRecordProcessService.selectByOrderNos(request.getComId(), request.getOrderNos()));
	}

	//订单同步到成功表
	@PostMapping(PayHandleUrl.URL_SyncOrderBySuccess)
	public Result SyncOrder(@RequestBody EntityXfRecordProcess entiyEntiyXfRecordProcess) {
		try {
			MqSaveOrderVo mqSaveOrderVo=new MqSaveOrderVo();
			mqSaveOrderVo.setComId(entiyEntiyXfRecordProcess.getComId());
			mqSaveOrderVo.setId(entiyEntiyXfRecordProcess.getId());
			mqSaveOrderVo.setMachNo(entiyEntiyXfRecordProcess.getDeviceNo());
			saveOrderProductService.saveOrder(JSON.toJSONString(mqSaveOrderVo));
		}catch (Exception e){
			StackTraceElement ste =e.getStackTrace()[0];
			log.info("请求方式:【{}】,入库成功表数据的数据:【{}】,异常的返回:【{}】,行号:【{}】","支付请求", entiyEntiyXfRecordProcess,e,ste.getLineNumber());
			return ResultUtil.error(ResultEnum.Err_3043);
		}
		return ResultUtil.success();
	}

}
